<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- <el-checkbox-group v-model="roleIds"> -->
    <!-- 注意：label决定当前选中的值 -->
    <!-- <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group> -->
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/getRoles'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
    this.loadRolesById()
  },
  methods: {
    // 点击确定
    async hSave() {
      const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
      console.log('assignRoles', res)
      this.$emit('success')
    },
    async loadRolesById() {
      const res = await getUserDetailById(this.curId)
      // console.log('getUserDetailById', res)
      this.roleIds = res.data.roleIds
    },
    async loadRoles() {
      const res = await getRoles({ page: 1, pagesize: 100 })
      this.list = res.data.rows
    },
    // 点击取消
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
